#/usr/bin/python
# coding: utf8

import time
from  PAM30 import PAM30
#import PAM30
import sys
import win32api
import win32con
import ConfigParser

reload(sys)
sys.setdefaultencoding("utf-8")


def ordd(str):
    '''
    解决键盘码不对应问题,不知道别的电脑有没有这个问题
    '''
    t = ord(str)
    #65到90是A到Z
    if(t>90):
        return t-32
    else:
        return t


def typeme(passw):
    '''
    模拟人工输入,自动输入参数字符串到焦点窗口
    '''
    psht = len(passw)
    for j in range(psht):
        win32api.keybd_event(ordd(passw[j]), 0, 0, 0)
        time.sleep(0.5)
        win32api.keybd_event(ordd(passw[j]), 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(0.5)


def lgTaobao(username, userpass):
    '''
    使用指定用户名密码（在IE）登录淘宝
    '''
    lgurl = 'https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Fi.taobao.com%2Fmy_taobao.htm%3Fspm%3Da1z0e.3.217351.2.9fe82b%26scm%3D1012.1.30.0'
    ie.navigate(lgurl)
    ie.setTextBox('TPL_username', username.decode('utf-8'))
    time.sleep(1)
    ie.setCheckBox('J_SafeLoginCheck', 0)
    time.sleep(1)
    bt = ie.findElement('button', 'id', 'J_SubmitStatic')
    ie.clickElement(bt)
    #ie.setTextBox('TPL_password', userpass)
    typeme(userpass)
    time.sleep(0.5)
    bt = ie.findElement('button', 'id', 'J_SubmitStatic')
    ie.clickElement(bt)
    if(ie.findText(username)):
        return True
    else:
        return False


def clearCart1():
    '''
    默认登陆状态，然后自动到taojinbi.taobao.com
    点击领取金币
    '''
    print 'clear cart'
    time.sleep(3)
    ie.stop()
    #jbi = ie.findElement('a', 'id', 'J_CoinGrantBtn')
    #isC = ie.clickElement(jbi)
    isC = ie.clickLink('J_CoinGrantBtn')
    if(isC):
        win32api.Beep(3000, 2000)
        return True
    else:
        win32api.Beep(300, 200)
        return False


def clearCart():
    '''
    清空支付购物车的订单,默认是在登录状态下
    '''
    isClickcart = ie.clickLink('mc-menu-hd')
    if(isClickcart):
        if(ie.setCheckBox('J_SelectAll', 1)):
            time.sleep(0.5)
            bdel = ie.findElement('span', 'id', 'J_DelSelect')#购物车全删除
            if(ie.clickElement(bdel)):
                time.sleep(0.5)#全删除
                #点击弹出框的确定
                #clickCancel = cModalPopUp.handlePopup("Confirm","确定")
                import winGuiAuto
                # 获取得的hwnd是一个列表
                hwnd=winGuiAuto.findTopWindows(wantedText="Microsoft Internet Explorer")
                # 获取得的control_list是一个列表，每个元素又是一个列表
                #这个列表中第一个是句柄,第二个是文本，第三个是窗口类型
                control_list=winGuiAuto.dumpWindow(hwnd[0])
                for control_item in control_list:
                    if(control_item[1]=='确定'):
                        # 单击确定按钮
                        winGuiAuto.clickButton(control_item[0])
            else:
                print 'del cart fail'
    else:
        #win32gui.MessageBox(0, '进入购物车失败！'.decode('utf-8'), 'Notice!', 0)
        print 'Enter cart fail'


config = ConfigParser.ConfigParser()
config.read('sys.ini')
taobaoid = config.get("global", "taobaoid")
loginpass = config.get("global", "loginpass")
paypass = config.get("global", "paypass")
ie = PAM30.PAMIE()
time.sleep(2)
lgTaobao(taobaoid, loginpass)
time.sleep(2)
if(clearCart()):
    print 'OK'
else:
    print 'bu ok'
